package freedom.solution.app.prueba;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import freedom.solution.app.restaurante.datatypes.DBDetallePedido;
import freedom.solution.app.restaurante.datatypes.DBclass;
import freedom.solution.app.restaurante.datatypes.DtoUsuario;
import freedom.solution.app.restaurante.service.Conexion;
import freedom.soluttion.app.restaurante.LazyAdapter;
import freedom.soluttion.app.restaurante.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;



public class DetallePedidoActivity extends Activity {
	// All static variables
//	static final String URL = "http://api.androidhive.info/music/music.xml";
	static final String KEY_ID = "id";
	static final String KEY_SUBTOTAL = "subtotal";
	static final String KEY_NOMBRE="nombre";
	static final String KEY_DESCRIPCION="descripcion";
	static final String KEY_CANTIDAD="cantidad";
	static final String KEY_PRECIO="precio_unitario";
	private ProgressDialog pDialog;
	ArrayList<HashMap<String, String>> detallePedido=new ArrayList<HashMap<String,String>>();
	ListView list;
    ListaProductosAdapter adapter;
    int id_mesa;
    Button btnNroMesa;
    DBclass dbclass;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mesa_main);
		
		btnNroMesa = (Button)findViewById(R.id.mesa_main_btnNroMesa);
		
		Bundle bundle = getIntent().getExtras();
		id_mesa=Integer.parseInt((bundle.getString("id_mesa")));
		detallePedido= new ArrayList<HashMap<String,String>>();
		  adapter=new ListaProductosAdapter(this, detallePedido);
		new cargarDetallePedido().execute();
		  dbclass = new DBclass(getApplicationContext());
		
        setTitle("Detalle Pedido - Mesa" + id_mesa );
        btnNroMesa.setText("Mesa "+id_mesa);

        //mostrar los tabs
        TabHost tabs = (TabHost)findViewById(android.R.id.tabhost);
        tabs.setup();
         
        TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
        spec.setContent(R.id.tab1);
        spec.setIndicator("Pedido",
                getResources().getDrawable(android.R.drawable.ic_btn_speak_now));
        tabs.addTab(spec);
         
        spec=tabs.newTabSpec("mitab2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Productos",
                getResources().getDrawable(android.R.drawable.ic_dialog_map));
        tabs.addTab(spec);
         
        tabs.setCurrentTab(0);	     
		
	}	
	
	  class cargarDetallePedido extends AsyncTask<String, String, String> {
			 
	        /**
	         * Before starting background thread Show Progress Dialog
	         * */
	        @Override
	        protected void onPreExecute() {
	            super.onPreExecute();
	            pDialog = new ProgressDialog(DetallePedidoActivity.this);
	            pDialog.setMessage("Cargando Detalle Pedido ...");
	            pDialog.setIndeterminate(false);
	            pDialog.setCancelable(false);
	            pDialog.show();
	        }
	 
	        /**
	         * getting Albums JSON
	         * */
	        protected String doInBackground(String... args) {
	            // Building Parameters
	    
	        	try{
	        		  ArrayList<DBDetallePedido>  lista_detalle_pedido= new ArrayList<DBDetallePedido>();
	      			lista_detalle_pedido= dbclass.getDetallePedido(id_mesa);
	      			 Iterator<DBDetallePedido> it=lista_detalle_pedido.iterator();
		      		  while ( it.hasNext() ) { 
		      				  Object objeto = it.next(); 
		      				  DBDetallePedido cta = (DBDetallePedido)objeto; 
	      				   
	      					
	      				HashMap<String, String> map = new HashMap<String, String>();
	      				//map.put(KEY_CODCLI,obtenerPersona[i].getCodcli());
	      				map.put(KEY_DESCRIPCION,""+ dbclass.getNomProductoXCodigo(cta.getId_producto()));
	      				map.put(KEY_CANTIDAD, cta.getCantidad()+"");
	      				map.put(KEY_SUBTOTAL, cta.getSubtotal()+"");
	      		
	      				
	      							
	      				detallePedido.add(map);
	      			
	      				
	      			}		
	        	} 
	    		catch (Exception e) 
	    		{
	    			//Toast.makeText(this, "No existe categorias", Toast.LENGTH_LONG).show();
	    		} 
	            return null;
	        }
	        
	     
	 
	        /**
	         * After completing background task Dismiss the progress dialog
	         * **/
	        protected void onPostExecute(String file_url) {
	            // dismiss the dialog after getting all albums
	            pDialog.dismiss();
	            // updating UI from Background Thread
	            runOnUiThread(new Runnable() {
	                public void run() {
	                    /**
	                     * Updating parsed JSON data into ListView
	                     * */
	                	list=(ListView)findViewById(R.id.list);
	            		
	            		// Getting adapter by passing xml data ArrayList
	                          
	                    list.setAdapter(adapter);

	                }
	            });
	 
	        }
	 
	    }
	  
	  public boolean onCreateOptionsMenu(Menu menu) {
	    	
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.menu_mesa, menu);
	        
	        return true;
	    }
	    
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	        case R.id.mnu_mesa_menu:
	     
	        	Intent i=new Intent(getApplicationContext(), SearchActivity.class);
	        	Intent i=new Intent(getApplicationContext(), ListViewImagesActivity.class);				
				startActivity(i); 
				
	            return true;
	        case R.id.mnu__mesa_facturar:
	        	
	        	Intent intent=new Intent(getApplicationContext(), FacturacionActivity.class);
	        	intent.putExtra("id_mesa", id_mesa);
	        	startActivity(intent);
	        
	            return true;
	        case R.id.mnu__mesa_cancelar:

	            return true;
	     
	        default:
	            return super.onOptionsItemSelected(item);
	        }
	    }
}